import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';

/// author：william on 2021/4/29 17:10
/// email：362661044@qq.com
class BusinessMessageLikeModel {
  final String likeService;
  final Map<String, dynamic> params;
  final bool isLike;
  BusinessMessageLikeModel({@required this.isLike, @required this.likeService, @required this.params});
}

class BusinessMessageLike extends StatefulWidget {
  final BusinessMessageLikeModel messageLike;
  const BusinessMessageLike({Key key, @required this.messageLike}) : super(key: key);
  @override
  _BusinessMessageLikeState createState() => _BusinessMessageLikeState();
}

class _BusinessMessageLikeState extends State<BusinessMessageLike> {
  bool isLike = false;

  @override
  void initState() {
    isLike = widget.messageLike.isLike ?? false;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant BusinessMessageLike oldWidget) {
    if (widget.messageLike.isLike != oldWidget.messageLike.isLike) {
      isLike = widget.messageLike.isLike;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        setState(() {
          isLike = !isLike;
        });
        onLike();
      },
      child: Container(
        padding: EdgeInsets.all(2),
        child: AnimatedSwitcher(
          duration: Duration(milliseconds: 300),
          child: Icon(
            isLike == false ? BaseIconData.x3NotLiked : BaseIconData.x3Liked,
            key: UniqueKey(),
            size: 18,
            color: isLike == false ? BaseBodyTheme.textColor : Color(0xffFF0451),
          ),
        ),
      ),
    );
  }

  Future<bool> onLike({bool isLike}) async {
    BaseHttpResultModel result = await dioUtil.fetch(
      widget.messageLike.likeService,
      params: widget.messageLike.params,
    );
    return result.success;
  }
}
